﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.VisualBasic;
using System.Data;

namespace Task_Network_Center
{
    /// <summary>
    /// Logique d'interaction pour FrmConfigTable.xaml
    /// </summary>
    public partial class FrmConfigTable : Window
    {

        public object[,] TempDepartementArray;
        public object[,] TempEquipeArray;

        static GCGDataBaseTableLib.EquipeTable Tequipe = new GCGDataBaseTableLib.EquipeTable();
        static GCGDataBaseTableLib.PrioriteTable Tpriorite = new GCGDataBaseTableLib.PrioriteTable();
        static GCGDataBaseTableLib.DepartementTable Tdepartement = new GCGDataBaseTableLib.DepartementTable();
        static GCGDataBaseTableLib.EmployeTable Temploye = new GCGDataBaseTableLib.EmployeTable();
        static GCGDataBaseTableLib.UpdateTable Tupdate = new GCGDataBaseTableLib.UpdateTable();

        int EquipeSelection = 0, PrioriteSelection = 0, DepartementSelection = 0, EmployeSelection = 0;
        bool EquipeAdd = false, PrioriteAdd = false, DepartementAdd = true, EmployeAdd = false;

        public FrmConfigTable()
        {
            InitializeComponent();
        }

        private void ConfigTable_Loaded(object sender, RoutedEventArgs e)
        {
            brdDepartement.Visibility = System.Windows.Visibility.Hidden;
            brdEmploye.Visibility = System.Windows.Visibility.Hidden;
            brdEquipe.Visibility = System.Windows.Visibility.Hidden;
            brdPriorite.Visibility = System.Windows.Visibility.Hidden;

            Task_Network_Center.MainDataSet mainDataSet = ((Task_Network_Center.MainDataSet)(this.FindResource("mainDataSet")));
            tbEquipe.DataContext = null;
            tbPriorite.DataContext = null;
            tbEquipe.DataContext = Tequipe.QueryTableReadable("SELECT * FROM tableconfigequipe ORDER BY equipe").DefaultView;
            tbPriorite.DataContext = Tpriorite.QueryTableReadable("SELECT * FROM tableconfigpriorite").DefaultView;
            tbDepartement.DataContext = null;
            tbDepartement.DataContext = Tdepartement.QueryTableReadable("SELECT * FROM tableconfigdepartement").DefaultView;
            tbEmploye.DataContext = null;
            tbEmploye.DataContext = Temploye.QueryTableReadable("SELECT * FROM tableemployees ORDER BY initial",TempDepartementArray, TempEquipeArray).DefaultView;

            for (int i = 0; i < TempEquipeArray.GetLength(1); i++)
            {
                cboEquipe.Items.Add(TempEquipeArray[1, i].ToString());
            }

            for (int i = 0; i < TempDepartementArray.GetLength(1); i++)
            {
                cboDepartement.Items.Add(TempDepartementArray[1, i].ToString());
            }
            }
        
        private void cmdEquipeAdd_Click(object sender, RoutedEventArgs e)
        {
            EquipeAdd = true;
            brdEquipe.Visibility = System.Windows.Visibility.Visible;
            txtEquipe.Text = "";
            tbEquipe.IsEnabled = false;
            txtEquipe.Focus();

        }

        private void cmdEquipeCancel_Click(object sender, RoutedEventArgs e)
        {
            brdEquipe.Visibility = System.Windows.Visibility.Hidden;
            txtEquipe.Text = "";
            tbEquipe.IsEnabled = true;
        }

        private void cmdEquipeAccepter_Click(object sender, RoutedEventArgs e)
        {
            if (EquipeAdd)
            {
                brdEquipe.Visibility = System.Windows.Visibility.Hidden;
                Tequipe.InsertRow(txtEquipe.Text);
                txtEquipe.Text = "";
                tbEquipe.IsEnabled = true;
                tbEquipe.DataContext = null;
                tbEquipe.DataContext = Tequipe.QueryTableReadable("SELECT * FROM tableconfigequipe ORDER BY equipe").DefaultView;

                TempEquipeArray = Tequipe.ListEquipeToStringArray();
                cboEquipe.Items.Clear();
                for (int i = 0; i < TempEquipeArray.GetLength(1); i++)
                {
                    cboEquipe.Items.Add(TempEquipeArray[1, i].ToString());
                }
                tbEmploye.DataContext = null;
                tbEmploye.DataContext = Temploye.QueryTableReadable("SELECT * FROM tableemployees ORDER BY initial", TempDepartementArray, TempEquipeArray);   

            }
            else if (!EquipeAdd)
            {
                brdEquipe.Visibility = System.Windows.Visibility.Hidden;
                Tequipe.UpdateRow(EquipeSelection, txtEquipe.Text);
                txtEquipe.Text = "";
                tbEquipe.IsEnabled = true;
                tbEquipe.DataContext = null;
                tbEquipe.DataContext = Tequipe.QueryTableReadable("SELECT * FROM tableconfigequipe ORDER BY equipe").DefaultView;

                TempEquipeArray = Tequipe.ListEquipeToStringArray();
                cboEquipe.Items.Clear();
                for (int i = 0; i < TempEquipeArray.GetLength(1); i++)
                {
                    cboEquipe.Items.Add(TempEquipeArray[1, i].ToString());
                }
                tbEmploye.DataContext = null;
                tbEmploye.DataContext = Temploye.QueryTableReadable("SELECT * FROM tableemployees ORDER BY initial", TempDepartementArray, TempEquipeArray);   
            }

        }

        private void cmdPrioriteAdd_Click(object sender, RoutedEventArgs e)
        {
            PrioriteAdd = true;
            brdPriorite.Visibility = System.Windows.Visibility.Visible;
            txtPriorite.Text = "";
            tbPriorite.IsEnabled = false;
            txtPriorite.Focus();
            
        }

        private void cmdPrioriteAccepter_Click(object sender, RoutedEventArgs e)
        {
            if (PrioriteAdd)
            {
                brdPriorite.Visibility = System.Windows.Visibility.Hidden;
                Tpriorite.InsertRow(txtPriorite.Text);
                txtPriorite.Text = "";
                tbPriorite.IsEnabled = true;
                tbPriorite.DataContext = null;
                tbPriorite.DataContext = Tpriorite.QueryTableReadable("SELECT * FROM tableconfigpriorite").DefaultView;
            }
            else if (!PrioriteAdd)
            {
                brdPriorite.Visibility = System.Windows.Visibility.Hidden;
                Tpriorite.UpdateRow(PrioriteSelection, txtPriorite.Text);
                txtPriorite.Text = "";
                tbPriorite.IsEnabled = true;
                tbPriorite.DataContext = null;
                tbPriorite.DataContext = Tpriorite.QueryTableReadable("SELECT * FROM tableconfigpriorite").DefaultView;
            }


        }

        private void cmdPrioriteCancel_Click(object sender, RoutedEventArgs e)
        {
            brdPriorite.Visibility = System.Windows.Visibility.Hidden;
            txtPriorite.Text = "";
            tbPriorite.IsEnabled = true;
        }

        private void cmdDepartementAdd_Click(object sender, RoutedEventArgs e)
        {
            DepartementAdd = true;
            brdDepartement.Visibility = System.Windows.Visibility.Visible;
            txtDepartement.Text = "";
            txtDescription.Text = "";
            tbDepartement.IsEnabled = false;
            txtDepartement.Focus();
        }

        private void cmdDepartementAccepter_Click(object sender, RoutedEventArgs e)
        {
            if (DepartementAdd)
            {
                brdDepartement.Visibility = System.Windows.Visibility.Hidden;
                Tdepartement.InsertRow(txtDepartement.Text, txtDescription.Text);
                txtDepartement.Text = "";
                txtDescription.Text = "";
                tbDepartement.IsEnabled = true;
                tbDepartement.DataContext = null;
                tbDepartement.DataContext = Tdepartement.QueryTableReadable("SELECT * FROM tableconfigdepartement");

                TempDepartementArray = Tdepartement.ListDepartementToStringArray();
                cboDepartement.Items.Clear();
                for (int i = 0; i < TempDepartementArray.GetLength(1); i++)
                {
                    cboDepartement.Items.Add(TempDepartementArray[1, i].ToString());
                }
                tbEmploye.DataContext = null;
                tbEmploye.DataContext = Temploye.QueryTableReadable("SELECT * FROM tableemployees ORDER BY initial", TempDepartementArray, TempEquipeArray);   

            }
            else if (!DepartementAdd)
            {
                brdDepartement.Visibility = System.Windows.Visibility.Hidden;
                Tdepartement.UpdateRow(DepartementSelection, txtDepartement.Text,txtDescription.Text);
                txtDepartement.Text = "";
                txtDescription.Text = "";
                tbDepartement.IsEnabled = true;
                tbDepartement.DataContext = null;
                tbDepartement.DataContext = Tdepartement.QueryTableReadable("SELECT * FROM tableconfigdepartement");

                TempDepartementArray = Tdepartement.ListDepartementToStringArray();
                cboDepartement.Items.Clear();
                for (int i = 0; i < TempDepartementArray.GetLength(1); i++)
                {
                    cboDepartement.Items.Add(TempDepartementArray[1, i].ToString());
                }
                tbEmploye.DataContext = null;
                tbEmploye.DataContext = Temploye.QueryTableReadable("SELECT * FROM tableemployees ORDER BY initial", TempDepartementArray, TempEquipeArray);   
            }

        }

        private void cmdDepartementCancel_Click(object sender, RoutedEventArgs e)
        {
            brdDepartement.Visibility = System.Windows.Visibility.Hidden;
            txtDepartement.Text = "";
            txtDescription.Text = "";
            tbDepartement.IsEnabled = true;
        }

        private void cmdEmployeAdd_Click(object sender, RoutedEventArgs e)
        {
            EmployeAdd = true;
            brdEmploye.Visibility = System.Windows.Visibility.Visible;
            txtNomComplet.Text = "";
            cboEquipe.SelectedIndex = -1;
            txtInitial.Text = "";
            cboDepartement.SelectedIndex = -1;
            txtCourriel.Text = "";
            tbEmploye.IsEnabled = false;
            txtNomComplet.Focus();

        }

        private void cmdEmployeCancel_Click(object sender, RoutedEventArgs e)
        {
            brdEmploye.Visibility = System.Windows.Visibility.Hidden;
            txtNomComplet.Text = "";
            cboEquipe.SelectedIndex = -1;
            txtInitial.Text = "";
            cboDepartement.SelectedIndex = -1;
            txtCourriel.Text = "";
            tbEmploye.IsEnabled = true;
        }

        private void cmdEmployeAccepter_Click(object sender, RoutedEventArgs e)
        {
            if (EmployeAdd)
            {
            brdEmploye.Visibility = System.Windows.Visibility.Hidden;
            Temploye.InserRow(txtNomComplet, cboEquipe, txtInitial, cboDepartement, txtCourriel, TempEquipeArray, TempDepartementArray);
            //Tupdate.InsertRow(newID, 0);
            txtNomComplet.Text = "";
            cboEquipe.SelectedIndex = -1;
            txtInitial.Text = "";
            cboDepartement.SelectedIndex = -1;
            txtCourriel.Text = "";
            tbEmploye.IsEnabled = true;
            tbEmploye.DataContext = null;
            tbEmploye.DataContext = Temploye.QueryTableReadable("SELECT * FROM tableemployees ORDER BY initial",TempDepartementArray, TempEquipeArray); 
            

            }
            else if (!EmployeAdd)
            {
            brdEmploye.Visibility = System.Windows.Visibility.Hidden;
            Temploye.UpdateRow(EmployeSelection,txtNomComplet, cboEquipe, txtInitial, cboDepartement, txtCourriel, TempEquipeArray, TempDepartementArray);
            txtNomComplet.Text = "";
            cboEquipe.SelectedIndex = -1;
            txtInitial.Text = "";
            cboDepartement.SelectedIndex = -1;
            txtCourriel.Text = "";
            tbEmploye.IsEnabled = true;
            tbEmploye.DataContext = null;
            tbEmploye.DataContext = Temploye.QueryTableReadable("SELECT * FROM tableemployees ORDER BY initial",TempDepartementArray, TempEquipeArray);  
            }

        }

        private void cmdEquipeModif_Click(object sender, RoutedEventArgs e)
        {
            if (tbEquipe.SelectedItem != null)
            {
                EquipeAdd = false;
                DataTable tmpTb = Tequipe.QueryTableReadable("SELECT * FROM tableconfigequipe WHERE id_config_equipe =" + EquipeSelection);
                txtEquipe.Text = tmpTb.Rows[0][1].ToString();
                brdEquipe.Visibility = System.Windows.Visibility.Visible;
                tbEquipe.IsEnabled = false;
                txtEquipe.Focus();
            }
            else
            {
                MessageBox.Show("Vous devez choisir une ligne...");
            }

        }

        private void tbEquipe_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            DataRowView drv = (DataRowView)tbEquipe.SelectedItem;
            if (drv != null)
            {
                EquipeSelection = Convert.ToInt32(drv.Row.ItemArray[0].ToString());
            }
        }

        private void tbPriorite_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            DataRowView drv = (DataRowView)tbPriorite.SelectedItem;
            if (drv != null)
            {
                PrioriteSelection = Convert.ToInt32(drv.Row.ItemArray[0].ToString());
            }
        }

        private void tbDepartement_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            DataRowView drv = (DataRowView)tbDepartement.SelectedItem;
            if (drv != null)
            {
                DepartementSelection = Convert.ToInt32(drv.Row.ItemArray[0].ToString());
            }
        }

        private void tbEmploye_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            DataRowView drv = (DataRowView)tbEmploye.SelectedItem;
            if (drv != null)
            {
                EmployeSelection = Convert.ToInt32(drv.Row.ItemArray[0].ToString());
            }
        }

        private void cmdPrioriteModif_Click(object sender, RoutedEventArgs e)
        {
            if (tbPriorite.SelectedItem != null)
            {
                PrioriteAdd = false;
                DataTable tmpTb = Tpriorite.QueryTableReadable("SELECT * FROM tableconfigpriorite WHERE id_config_priorite =" + PrioriteSelection);
                txtPriorite.Text = tmpTb.Rows[0][1].ToString();
                brdPriorite.Visibility = System.Windows.Visibility.Visible;
                tbPriorite.IsEnabled = false;
                txtPriorite.Focus();
            }
            else
            {
                MessageBox.Show("Vous devez choisir une ligne...");
            }

        }

        private void cmdDepartementModif_Click(object sender, RoutedEventArgs e)
        {
            if (tbDepartement.SelectedItem != null)
            {
                DepartementAdd = false;
                DataTable tmpTb = Tdepartement.QueryTableReadable("SELECT * FROM tableconfigdepartement WHERE id_config_departement =" + DepartementSelection);
                txtDepartement.Text = tmpTb.Rows[0][1].ToString();
                txtDescription.Text = tmpTb.Rows[0][2].ToString();
                brdDepartement.Visibility = System.Windows.Visibility.Visible;
                tbDepartement.IsEnabled = false;
                txtDepartement.Focus();
            }
            else
            {
                MessageBox.Show("Vous devez choisir une ligne...");
            }
        }

        private void cmdEmployeModif_Click(object sender, RoutedEventArgs e)
        {
            if (tbEmploye.SelectedItem != null)
            {
                EmployeAdd = false;
                DataTable tmpTb = Temploye.QueryTableReadable("SELECT * FROM tableemployees WHERE id_employes =" + EmployeSelection, TempDepartementArray, TempEquipeArray);

                txtNomComplet.Text = tmpTb.Rows[0][1].ToString();

                if (tmpTb.Rows[0][2].ToString() != "")
                {
                    for (int i = 0; i < TempEquipeArray.GetLength(1); i++)
                    {
                        if (tmpTb.Rows[0][2].ToString() == TempEquipeArray[1, i].ToString())
                        {
                            cboEquipe.SelectedIndex = Convert.ToInt32(TempEquipeArray[2, i]);
                        }
                    }
                }
                else
                {
                    cboEquipe.SelectedIndex = -1;
                }


                txtInitial.Text = tmpTb.Rows[0][3].ToString();

                if (tmpTb.Rows[0][4].ToString() != "")
                {
                    for (int i = 0; i < TempDepartementArray.GetLength(1); i++)
                    {
                        if (tmpTb.Rows[0][4].ToString() == TempDepartementArray[1, i].ToString())
                        {
                            cboDepartement.SelectedIndex = Convert.ToInt32(TempDepartementArray[2, i]);
                        }
                    }
                }
                else
                {
                    cboDepartement.SelectedIndex = -1;
                }

                txtCourriel.Text = tmpTb.Rows[0][5].ToString();

                brdEmploye.Visibility = System.Windows.Visibility.Visible;
                tbEmploye.IsEnabled = false;
                txtNomComplet.Focus();
            }
            else
            {
                MessageBox.Show("Vous devez choisir une ligne...");
            }

        }

        private void cmdQuit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void tbEquipe_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            EquipeAdd = false;
            DataTable tmpTb = Tequipe.QueryTableReadable("SELECT * FROM tableconfigequipe WHERE id_config_equipe =" + EquipeSelection);
            txtEquipe.Text = tmpTb.Rows[0][1].ToString();
            brdEquipe.Visibility = System.Windows.Visibility.Visible;
            tbEquipe.IsEnabled = false;
            txtEquipe.Focus();
        }

        private void tbPriorite_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            PrioriteAdd = false;
            DataTable tmpTb = Tpriorite.QueryTableReadable("SELECT * FROM tableconfigpriorite WHERE id_config_priorite =" + PrioriteSelection);
            txtPriorite.Text = tmpTb.Rows[0][1].ToString();
            brdPriorite.Visibility = System.Windows.Visibility.Visible;
            tbPriorite.IsEnabled = false;
            txtPriorite.Focus();
        }

        private void tbDepartement_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            DepartementAdd = false;
            DataTable tmpTb = Tdepartement.QueryTableReadable("SELECT * FROM tableconfigdepartement WHERE id_config_departement =" + DepartementSelection);
            txtDepartement.Text = tmpTb.Rows[0][1].ToString();
            txtDescription.Text = tmpTb.Rows[0][2].ToString();
            brdDepartement.Visibility = System.Windows.Visibility.Visible;
            tbDepartement.IsEnabled = false;
            txtDepartement.Focus();
        }

        private void tbEmploye_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            EmployeAdd = false;
            DataTable tmpTb = Temploye.QueryTableReadable("SELECT * FROM tableemployees WHERE id_employes =" + EmployeSelection, TempDepartementArray, TempEquipeArray);

            txtNomComplet.Text = tmpTb.Rows[0][1].ToString();

            if (tmpTb.Rows[0][2].ToString() != "")
            {
                for (int i = 0; i < TempEquipeArray.GetLength(1); i++)
                {
                    if (tmpTb.Rows[0][2].ToString() == TempEquipeArray[1, i].ToString())
                    {
                        cboEquipe.SelectedIndex = Convert.ToInt32(TempEquipeArray[2, i]);
                    }
                }
            }
            else
            {
                cboEquipe.SelectedIndex = -1;
            }


            txtInitial.Text = tmpTb.Rows[0][3].ToString();

            if (tmpTb.Rows[0][4].ToString() != "")
            {
                for (int i = 0; i < TempDepartementArray.GetLength(1); i++)
                {
                    if (tmpTb.Rows[0][4].ToString() == TempDepartementArray[1, i].ToString())
                    {
                        cboDepartement.SelectedIndex = Convert.ToInt32(TempDepartementArray[2, i]);
                    }
                }
            }
            else
            {
                cboDepartement.SelectedIndex = -1;
            }

            txtCourriel.Text = tmpTb.Rows[0][5].ToString();

            brdEmploye.Visibility = System.Windows.Visibility.Visible;
            tbEmploye.IsEnabled = false;
            txtNomComplet.Focus();
        }



    }
}
